﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the Y, U, V values are filtered instead of R G B
    /// </summary>
    public class YUVFilter : BaseForeBackgroundColorFilter
    {
        protected FloatInterval m_YInterval = new FloatInterval(0.0f, 1.0f);
        protected FloatInterval m_UInterval = new FloatInterval(0.0f, 1.0f);
        protected FloatInterval m_VInterval = new FloatInterval(0.0f, 1.0f);

        /// <summary>
        /// Default constructor
        /// </summary>
        public YUVFilter()
        {
        }

        /// <summary>
        /// specify intervals directly
        /// </summary>
        /// <param name="_y"></param>
        /// <param name="_u"></param>
        /// <param name="_v"></param>
        public YUVFilter(FloatInterval _y, FloatInterval _u, FloatInterval _v)
        {
            m_YInterval = _y;
            m_UInterval = _u;
            m_VInterval = _v;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_yuvFilter"></param>
        internal YUVFilter(YUVFilter _yuvFilter)
        {
            this.m_YInterval = _yuvFilter.m_YInterval;
            this.m_UInterval = _yuvFilter.m_UInterval;
            this.m_VInterval = _yuvFilter.m_VInterval;
        }

        /// <summary>
        /// Gets or sets the Y.
        /// </summary>
        /// <value>The Y.</value>
        public FloatInterval Y
        {
            get { return m_YInterval; }
            set { m_YInterval = value; }
        }

        /// <summary>
        /// Gets or sets the U.
        /// </summary>
        /// <value>The U.</value>
        public FloatInterval U
        {
            get { return m_UInterval; }
            set { m_UInterval = value; }
        }

        /// <summary>
        /// Gets or sets the V.
        /// </summary>
        /// <value>The V.</value>
        public FloatInterval V
        {
            get { return m_VInterval; }
            set { m_VInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            YUV yuv = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    yuv = new YUV(bitmap.GetPixel(x,y));

                    if (m_YInterval.Contains(yuv.Y)
                        && m_UInterval.Contains(yuv.U)
                        && m_VInterval.Contains(yuv.V))
                    {
                        bitmap.SetPixel(x, y, yuv.ToColor());

                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// Used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new YUVFilter(this);
        }
    }
}
